package com.quanquan.store.alipay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.annotation.PostConstruct;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.quanquan.store.shenxinpay.utils.FinancialUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



@Component
public class Alipay {
	
	private static Logger logger = LoggerFactory.getLogger(Alipay.class);

	private AlipayClient client;

	@PostConstruct
	public void initClient() {
		client = new DefaultAlipayClient(config.getServerUrl(), config.getAppId(), config.getPrivateKey(),
				config.getFormat(), config.getCharset(),config.getPublicKey(), config.getSignType());
	}

	@Autowired
	private AlipayConfing config;

	// @Autowired
	// private SequenceFactoryBean sequenceFactoryBean;

	public boolean verifyNotifyParams(Map<String, String> params) {
		try {
			return AlipaySignature.rsaCheckV1(params, config.getPublicKey(), config.getCharset(), config.getSignType());
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return false;
	}

	public String createOauthUrlForCode(String redirectUri) {
		try {
			redirectUri = URLEncoder.encode(redirectUri, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return new StringBuffer().append("https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?").append("app_id=")
				.append(config.getAppId()).append("&redirect_uri=").append(redirectUri)
				.append("&scope=auth_base&state=STATE").toString();
	}

	public AlipaySystemOauthTokenResponse getOauthToken(String authCode) throws Exception {
		AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
		request.setCode(authCode);
		request.setGrantType("authorization_code");
		AlipaySystemOauthTokenResponse oauthTokenResponse = client.execute(request);
		return oauthTokenResponse;
	}

	public String alipayPage(Integer payFee,String orderNum, String returnUrl) throws Exception {
		String timeout_express = "2m";
		String product_code = "QUICK_WAP_WAY";
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
		// 封装请求支付信息
		AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
		model.setOutTradeNo(orderNum);
		model.setSubject("停车费");
		model.setTotalAmount(FinancialUtils.getYuan(payFee) + "");
		model.setBody("停车费");
		model.setTimeoutExpress(timeout_express);
		model.setProductCode(product_code);
		alipayRequest.setBizModel(model);
		// 设置异步通知地址
		alipayRequest.setNotifyUrl(config.getPayNotifyUrl());
		// 设置同步地址
		alipayRequest.setReturnUrl(returnUrl);
		AlipayTradeWapPayResponse response= client.pageExecute(alipayRequest);
		logger.info("支付宝支付页面：{}",response.getMsg());
		return client.pageExecute(alipayRequest).getBody();
	}

	public AlipayClient getClient() {
		return client;
	}

	public void setClient(AlipayClient client) {
		this.client = client;
	}

}
